package com.brillilab.starter.controller.aio.kit;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.pa.kit.SampleAddMorePa;
import com.brillilab.domain.pa.kit.SampleAddPa;
import com.brillilab.domain.vo.kit.SampleSpecVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import com.brillilab.service.logic.kit.SampleLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
/**
 * 样品管理
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/sample/add")
public class SampleAddController {
	@Autowired
	private SampleLogic sampleLogic;
	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private IInventoryRecordService inventoryRecordService;
	/**
	 * 新建单样品
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/one", method = RequestMethod.POST)
	public ResponseVo addSampleOne(@RequestHeader(name=StarterConstant.TOKEN) String token, @RequestBody SampleAddPa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		pa.setCreatorId(urs.getLabMemberId());
		SampleSpecVo specVo = sampleLogic.addSampleOne(pa);
		inventoryRecordService.add(urs, specVo.getKitId(), InventoryRecordEnum.CREATE);
		return ResponseVo.success(specVo);
	}
	
	/**
	 * 新建多样品
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/more", method = RequestMethod.POST)
	public ResponseVo addSampleMore(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody SampleAddMorePa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		pa.setCreatorId(urs.getLabMemberId());
		List<SampleSpecVo> list = sampleLogic.addSampleMore(pa);
		List<Long> collect = list.stream().map(SampleSpecVo::getKitId).collect(Collectors.toList());
		inventoryRecordService.add(urs, collect, InventoryRecordEnum.CREATE);
		return ResponseVo.success(list);
	}

	
}
